﻿using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using KinonekoSoftware.UI;
using Microsoft.Xaml.Behaviors;

namespace KinonekoSoftware.Studio.Contents
{
    public class DraggableBorder : Border
    {
        private static readonly SolidColorBrush Transparent = new SolidColorBrush(Colors.Transparent);
        
        protected override void OnRender(DrawingContext drawingContext)
        {

            base.OnRender(drawingContext);
            
            var rect   = new Rect(0, 0, ActualWidth, ActualHeight);
            var border = new SolidColorBrush(Colors.White);
            var corner = new SolidColorBrush(Colors.BlueViolet);


            //
            // 背景：透明
            // 边框: 白
            drawingContext.DrawRectangle(Transparent, new Pen(border, 2), rect);

            //
            // 边角
            //
            drawingContext.DrawEllipse(corner, null, new Point(0, -0), 8, 8);
            drawingContext.DrawEllipse(corner, null, new Point(ActualWidth, 0), 8, 8);
            drawingContext.DrawEllipse(corner, null, new Point(0, ActualHeight), 8, 8);
            drawingContext.DrawEllipse(corner, null, new Point(ActualWidth, ActualHeight), 8, 8);
        }

    }

    public class DraggableCanvasBehavior : Behavior<Canvas>
    {
        private bool                   _capture;
        private IImagePositionReceiver _imagePositionReceiver;
        
        protected override void OnAttached()
        {
            AssociatedObject.MouseUp    += OnMouseUp;
            AssociatedObject.MouseDown  += OnMouseDown;
            AssociatedObject.MouseLeave += OnMouseLeave;
            AssociatedObject.Loaded     += OnLoaded;
            
            
            base.OnAttached();
        }

        protected override void OnDetaching()
        {
            AssociatedObject.MouseUp    -= OnMouseUp;
            AssociatedObject.MouseDown  -= OnMouseDown;
            AssociatedObject.MouseLeave -= OnMouseLeave;
            AssociatedObject.Loaded     -= OnLoaded;
            base.OnDetaching();
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            if (AssociatedObject.DataContext is IImagePositionReceiver vm)
            {
                //
                // 设置画布大小
                vm.CanvasSize          = AssociatedObject.RenderSize;
                _imagePositionReceiver = vm;
            }
        }


        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            if(_capture)
                AssociatedObject.MouseMove -= OnMouseMove;

            _capture = false;
            AssociatedObject.ReleaseMouseCapture();
            
        }
        
        private void OnMouseUp(object sender, MouseButtonEventArgs e)
        {
            if(_capture)
                AssociatedObject.MouseMove -= OnMouseMove;

            _capture = false;
            AssociatedObject.ReleaseMouseCapture();
        }
        
        private void OnMouseMove(object sender, MouseEventArgs e)
        {
            var pos      = e.GetPosition(AssociatedObject);
            _imagePositionReceiver.SetDeltaPosition(pos.X / AssociatedObject.ActualWidth, pos.Y / AssociatedObject.ActualHeight);
            e.Handled = true;
        }
        
        
        private void OnMouseDown(object sender, MouseButtonEventArgs e)
        {
            if (AssociatedObject.DataContext is not IImagePositionReceiver vm)
            {
                return;
            }

            var pos = e.GetPosition(AssociatedObject);
            vm.SetDeltaPosition(pos.X / AssociatedObject.ActualWidth, pos.Y / AssociatedObject.ActualHeight);
            e.Handled                  =  true;
            _capture                   =  true;
            AssociatedObject.MouseMove += OnMouseMove;
        }
    }

    [Associate(View = typeof(ImageEditorView), ViewModel = typeof(ImageEditorViewModel))]
    public partial class ImageEditorView
    {
        public ImageEditorView()
        {
            InitializeComponent();
        }
    }
}